<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="AdvertiseFormData" class="demo-form-inline">
                <el-form-item>
                    <el-input v-model="AdvertiseFormData.name" placeholder="请输入名字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        align="center"
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="广告编号"
                            align="center"
                            width="50">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="广告名字"
                            align="center"
                            sortable
                            width="101">
                    </el-table-column>

                    <el-table-column
                            prop="smsAdvertisePosition.smsName"
                            label="广告位置"
                            sortable
                            width="106">
                    </el-table-column>
                    <el-table-column
                            prop="pic"
                            label="图片"
                            align="center"
                            width="120"
                            height="80"
                    >
                        <template slot-scope="scope">
                            <img :src="scope.row.pic" width="100" height="80" align="center">
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="startTime"
                            label="开始时间"
                            sortable
                            align="center"
                            width="150">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间"
                            sortable
                            align="center"
                            width="150">
                    </el-table-column>
                    <el-table-column
                            prop="status"
                            label="上下线状态"
                            sortable
                            align="center"
                            :formatter="statu"
                            width="130">
                    </el-table-column>
                    <el-table-column
                            prop="clickCount"
                            label="点击数"
                            align="center"
                            sortable
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="orderCount"
                            label="下单数"
                            align="center"
                            sortable
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="url"
                            label="链接地址"
                            align="center"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注"
                            align="center"
                            width="100">
                    </el-table-column>
                    <el-table-column label="操作" width="120">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle
                                       @click="selectById(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle
                                       @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>


            </template>
        </el-card>


        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
                :before-close="handleClose"
        >
            <!--            model:表单数据-->
            <el-form label-width="100px" :model="AdvertiseForm" :rules="formRules" ref="formRef">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="AdvertiseForm.id"></el-input>
                </el-form-item>
                <el-form-item label="广告名字" prop="name" :error="existError">
                    <el-input v-model="AdvertiseForm.name" @blur="nameUnique"></el-input>
                </el-form-item>
                <el-form-item label="广告位置">
                    <el-select v-model="AdvertiseForm.positionId" placeholder="请选择广告位置">
                        <el-option
                                v-for="itemes in positionArray"
                                :key="itemes.id"
                                :label="itemes.smsName"
                                :value="itemes.id">
                            <span style="float: left">{{ itemes.id}}</span>
                            <span style="float: right">{{ itemes.smsName }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="图片">
                    <!--                    <el-input v-model="AdvertiseForm.pic"></el-input>-->
                    <el-upload
                            class="adatar-uploader"
                            action="http://localhost:1000/marketing/advertise/uploadpicture"
                            :show-file-list="false"
                            :headers="{'token':token}"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                        <img v-model="AdvertiseForm.pic" v-if="AdvertiseForm.pic" :src="AdvertiseForm.pic" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                        <el-date-picker size="small" placeholder="请选择" v-model="AdvertiseForm.startTime"
                                        type="datetime" :picker-options="StartPickerDisabled" format="yyyy-MM-dd HH:mm:ss"  >
                        </el-date-picker>




                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker size="small" placeholder="请选择"
                                    :picker-options="EndPickerDisabled" v-model="AdvertiseForm.endTime" type="datetime"
                                    format="yyyy-MM-dd  HH:mm:ss">
                    </el-date-picker>







                </el-form-item>
                <el-form-item label="上下线状态">
                    <el-radio v-model="AdvertiseForm.status" :label="0" value="0" checked="true">上线</el-radio>
                    <el-radio v-model="AdvertiseForm.status" :label="1" value="1">下线</el-radio>
                </el-form-item>
                <el-form-item label="点击数">
                    <el-input v-model="AdvertiseForm.clickCount"></el-input>
                </el-form-item>
                <el-form-item label="下单数">
                    <el-input v-model="AdvertiseForm.orderCount"></el-input>
                </el-form-item>
                <el-form-item label="链接地址">
                    <el-input v-model="AdvertiseForm.url=AdvertiseForm.pic"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="AdvertiseForm.note"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="reset">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>


    </div>
</template>
<script>
    export default {
        name: "Advertise",
        data() {


            return {
                AdvertiseForm:{
                startTime:'',
                    endtTime:'',
                    pic:"",
                },
                StartPickerDisabled: {
                    //开始时间限制
                    disabledDate: time => {
                        let beginVal = this.AdvertiseForm.endTime;
                        if (beginVal) {
                            return time.getTime() > beginVal;
                        }
                    }
                },
                EndPickerDisabled: {
                    //结束时间限制
                    disabledDate: time => {
                        let beginVal = this.AdvertiseForm.startTime;
                        if (beginVal) {
                            return time.getTime() < beginVal;
                        }
                    }
                },








            flag: true,

                token: "",
                status: 0,
                editDialogVisible: false,
                //条件搜索对象
                AdvertiseFormData: {},
                //表格数据
                tableData: [],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage: 1,
                //总条数
                total: 0,
                //广告管理的查询表单
                AdvertiseForm: {},
                //位置数组
                positionArray: [],
                //错误信息提示
                existError: "",
                //表单校验
                formRules: {
                    name: [{required: true, message: '请输入广告名', trigger: 'blur'},
                        // {validator: checkRepeat, trigger: 'blur'}
                    ],
                    positionId: [{required: true, message: "请输入位置", trigger: 'blur'}],
                    status: [{required: true, message: "请输入状态", trigger: 'blur'}]
                },

            }
        },
        //表单加载时就执行
        created() {
            this.selectall();
            this.allselectlist();
            this.token = sessionStorage.getItem("token")

        },
        methods: {
            //提交增加和修改的表单校验
            formSubmit() {
                var that = this;
                this.$refs.formRef.validate((valid) => {
                    if (valid && that.flag) {
                        if (this.AdvertiseForm.id != null) {
                            this.$http.put("marketing/advertise/updateadvertise", this.AdvertiseForm).then(function (result) {
                                if (result.data.code === 2000) {
                                    that.editDialogVisible = false;
                                    that.selectall();
                                } else {
                                    that.selectall();
                                }
                            })

                        } else {
                            this.$http.post("marketing/advertise/insertadvertise", this.AdvertiseForm).then(function (result) {
                                if (result.data.code === 2000) {
                                    that.editDialogVisible = false;
                                    that.selectall();
                                }
                            })
                        }
                    } else {
                        return false;
                    }
                });

            },
            //查询所有
            selectall() {
                var that = this;
                this.$http.post(`marketing/advertise/selectpage/${this.currentPage}/${this.pageSize}`, this.AdvertiseFormData).then(function (returnResult) {
                    that.total = returnResult.data.result.total;
                    that.tableData = returnResult.data.result.records;
                    console.log(that.tableData)

                })
            },
            chaxun() {
                /*var that=this;
                this.$http.get(`marketing/advertise/selectall`,this.AdvertiseFormData).then(function () {
                    that.selectall()
                })*/
                var that = this;
                that.selectall()
            },
            //打开弹出框的弹出层
            insert() {
                this.editDialogVisible = true;
                this.AdvertiseForm = {};
            },
            //删除
            deleteById(id) {
                var that = this;
                this.$http.delete("marketing/advertise/deleteadvertise?id=" + id).then(function () {
                    that.selectall();
                    that.$message.success("删除成功");
                })
            },
            //通过id查找回显
            selectById(id) {
                var that = this;
                this.editDialogVisible = true;
                this.$http.get(`marketing/advertise/selectbyid/${id}`).then(function (returnResult) {
                    that.AdvertiseForm = returnResult.data.result.previews;
                    console.log(returnResult)
                })
            },
            //当前页条数
            handleSizeChange(v) {
                this.pageSize = v;
                this.selectall();
            },
            //当前页
            handleCurrentChange(v) {
                this.currentPage = v;
                this.selectall();
            },
            //状态
            statu: function (row, column) {
                return row.status == '0' ? "上线" : "下线";

            },
            //查询所以
            allselectlist() {
                var that = this;
                this.$http.get("/marketing/position/selectall").then(function (returnResult) {
                    that.positionArray = returnResult.data.result.allPosition;
                })
            },
            //广告名唯一性校验
            nameUnique() {
                var that = this;
                that.flag=true;
                this.$http.post(`/marketing/advertise/nameunique`, this.AdvertiseForm).then(function (result) {
                    console.log(result)
                    if (result.data.code != 2000) {
                        that.flag = false;
                    }
                    that.existError = result.data.message;
                });
            },
            //可以消除红色字体
            reset() {
                this.editDialogVisible = false;
                this.$refs.formRef.resetFields();
            },
            //提示框
            handleClose() {
                this.editDialogVisible = false;
                this.$refs.formRef.resetFields();
            },
            //文件上传
            handleAvatarSuccess(res, file) {
                this.AdvertiseForm.pic = res.result.path;
                this.$forceUpdate();
            },
            //文件上传校验
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>